/**
 * Add network downloading to your app.
 */

<!-- Put this in AndroidManifest.xml, above <application> -->
    <!-- 
    Only a n00b forgets to declare internet permission on a feed reader.
    -->
    <uses-permission android:name="android.permission.INTERNET" />


<!-- Put this in AndroidManifest.xml, above </application> -->
        <!-- 
        Fetches network content in a background task and provides it
        to the content Views. 
        -->
        <service
            android:name=".NetworkDownloadService"
            android:exported="false" />


/**
 * Copy these three files into your project source code directory:
 */
Constants.java
PicasaPullParser.java
NetworkDownloadService.java


/**
 * Add this constant into class DummyContent.
 */
public static final String ACTION_UPDATE = "com.example.acamp.fivewhat.dummy.update";


/**
 * Add this method into class DummyContent.
 */
public static void addItem(String key, String content) {
    addItem(new DummyItem(key, content));
}


/**
 * Modify DummyItem.toString() to extract only the file name from a URL.
 */
@Override
public String toString() {
    return content.substring(
            content.lastIndexOf('/')+1
            );
}


/**
 * Change DummyContent.init() to change:
 * new ListAssetsTask().execute(context);
 * to:
 */
// adapted from Picasa sample
Intent initIntent = new Intent(context, NetworkDownloadService.class);
Uri localUri = Uri.parse(Constants.PICASA_RSS_URL);
initIntent.setData(localUri);
context.startService(initIntent);

/**
 * In DummyContent.java, delete class ListAssetsTask.
 */

/**
 * Declare a BroadcastReceiver close to the top of PicListFragment.java
 * before: public interface Callbacks {
 */
// Instantiate a Broadcast Receiver so that the Network Download Service
// can message the List Fragment that the array backing the ListView has
// been updated.
private BroadcastReceiver mReceiver;

/**
 * In PicListFragment.onCreate(), add code to the end of the method
 * to register the BroadcastReceiver.
 */
// Set up the broadcast mReceiver so that the ArrayAdapter can update.
IntentFilter filter = new IntentFilter();
filter.addAction(DummyContent.ACTION_UPDATE);

mReceiver = new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {
		@SuppressWarnings("unchecked")
		ArrayAdapter<DummyContent.DummyItem> aa = (ArrayAdapter<DummyItem>) getListAdapter();
		aa.notifyDataSetChanged();
	}
};
getActivity().registerReceiver(mReceiver, filter);

/**
 * Implement PicListFragment.onStop().
 * Unregister the BroadcastReceiver.
 */
@Override
public void onDestroy() {
    getActivity().unregisterReceiver(mReceiver);
    super.onDestroy();
}


/**
 * In class PicDetailFragment, add GetImageTask.
 */
public final class GetImageTask extends AsyncTask<String, Void, Bitmap> {
	private static final String LOG_TAG = "GetImageTask";
	
    @Override
    protected Bitmap doInBackground(String... params) {
        String picUrlStr = params[0];
        URL picUrl;
        InputStream is;
        Bitmap b = null;
        
        if (TextUtils.isEmpty(picUrlStr)) {
            return null;
        }
        
        try {
            picUrl = new URL(picUrlStr);
            is = (InputStream) picUrl.getContent();
            b = BitmapFactory.decodeStream(is);
        } catch (MalformedURLException e) {
            Log.e(LOG_TAG, "Malformed URL", e);
        } catch (IOException e) {
            Log.e(LOG_TAG, "IO Exception", e);
        }
        return b;
    }
} // GetImageTask

/**
 * In PicDetailFragment.onCreateView(), call GetImageTask.
 */
if (mItem != null) {
    final String LOG_TAG = "Create View";
    Bitmap b = null;
    GetImageTask git = new GetImageTask();
    try {
        b = git.execute(mItem.content).get();
        ((ImageView) rootView.findViewById(R.id.pic_detail_pic))
                .setImageBitmap(b);
    } catch (InterruptedException e) {
        Log.e(LOG_TAG, "Interrupted Exception", e);
    } catch (ExecutionException e) {
        Log.e(LOG_TAG, "Execution Exception", e);
    }
}

